<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备编号" prop="deviceCode">
      <el-input v-model="dataForm.deviceCode" placeholder="设备编号"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="deviceName">
      <el-input v-model="dataForm.deviceName" placeholder="设备名称"></el-input>
    </el-form-item>
      <el-form-item label="抓拍开始时间" prop="dateList">
      <el-date-picker
        v-model="dataForm.dateList"
        type="datetimerange"
        value-format = "yyyy-MM-dd HH:mm:ss"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
      </el-form-item>
<!--    <el-form-item label="抓拍开始时间" prop="snapStartTime">-->
<!--      <el-input v-model="dataForm.snapStartTime" placeholder="抓拍开始时间"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="抓拍结束时间" prop="snapEndTime">-->
<!--      <el-input v-model="dataForm.snapEndTime" placeholder="抓拍结束时间"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="是否重点人员 1 重点 2 陌生人" prop="flag">-->
<!--      <el-input v-model="dataForm.flag" placeholder="是否重点人员 1 重点 2 陌生人"></el-input>-->
<!--    </el-form-item>-->

      <el-form-item label="人员类型" prop="flag">
        <el-select v-model="dataForm.flag" clearable placeholder="请选择">
          <el-option
            v-for="item in flags"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="传输协议" prop="protocol">
      <el-input v-model="dataForm.protocol" placeholder="传输协议"></el-input>
    </el-form-item>
    <el-form-item label="设备位置" prop="deviceLocation">
      <el-input v-model="dataForm.deviceLocation" placeholder="设备位置"></el-input>
    </el-form-item>
    <el-form-item label="抓拍策略" prop="snapStrategy">
      <el-input v-model="dataForm.snapStrategy" placeholder="抓拍策略"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/snap'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: '',
          deviceCode: '',
          deviceName: '',

          snapStartTime: '',

          snapEndTime: '',

          flag: '',

          protocol: '',

          deviceLocation: '',

          snapStrategy: '',

          createTime: '',

          updateTime: '',

          delFlag: '',
          dateList:[],

        },
        flags: [{
          value: '1',
          label: '重点人员'
        }, {
          value: '2',
          label: '陌生人员'
        }],
        dataRule: {
          deviceCode: [
            { required: true, message: '设备编号不能为空', trigger: 'blur' }
          ],

          deviceName: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],
          dateList: [
            { type:'array',required: true, message: '请选择抓拍时间范围', trigger: 'change' }
          ],

          flag: [
            { required: true, message: '是否重点人员 1 重点 2 陌生人不能为空', trigger: 'blur' }
          ],

          protocol: [
            { required: true, message: '传输协议不能为空', trigger: 'blur' }
          ],

          deviceLocation: [
            { required: true, message: '设备位置不能为空', trigger: 'blur' }
          ],

          snapStrategy: [
            { required: true, message: '抓拍策略不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
              //时间回显处理 将data 返回的时间push进去
              let tmpArr = []
              tmpArr.push(this.dataForm.snapStartTime)
              tmpArr.push(this.dataForm.snapEndTime)
              this.dataForm.dateList = tmpArr
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
              this.dataForm.snapStartTime = this.dataForm.dateList[0];
              this.dataForm.snapEndTime = this.dataForm.dateList[1];
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
              this.dataForm.snapStartTime = this.dataForm.dateList[0];
              this.dataForm.snapEndTime = this.dataForm.dateList[1];
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
